package org.jeecg.modules.util;

import org.apache.commons.lang3.StringUtils;

public class CRCUtil {
    /**
     * CRC16位校验
     *
     * @param arr_buff
     * @return
     */
    public static String[] getCrc16(byte[] arr_buff) {
        String[] crcArr = new String[2];
        int len = arr_buff.length;
        int crc = 0xFFFF;
        int i, j;
        for (i = 0; i < len; i++) {
            crc = ((crc & 0xFF00) | (crc & 0x00FF) ^ (arr_buff[i] & 0xFF));
            for (j = 0; j < 8; j++) {
                if ((crc & 0x0001) > 0) {
                    crc = crc >> 1;
                    crc = crc ^ 0xA001;
                } else
                    crc = crc >> 1;
            }
        }
        crcArr[0] = StringUtils.leftPad(Integer.toHexString((crc & 0xFF00) >> 8), 2, "0");
        crcArr[1] = StringUtils.leftPad(Integer.toHexString(crc & 0x00FF), 2, "0");
        return crcArr;
    }
}
